const app = require('../app')
const { REQUIRED, EXISTS, NOEXISTS, INCORRENT, UNAUTHORIZATION, NOPOWER } = require('../config/error.js')

// 监听error事件
app.on('error', (error , ctx) =>{
  let code = 0
  let message = ''

  switch (error){
    case REQUIRED:
      code = -1001
      message = '用户名或密码不能为空~'
      break
    case EXISTS:
      code = -1002
      message = '该用户名已经存在~'
      break
    case NOEXISTS:
      code = -1003
      message = '该用户不存在~'
      break

    case INCORRENT:
      code = -1004
      message = "密码错误~"
      break

    case UNAUTHORIZATION:
      code = -1005 
      message = "token已经过期"
      break

    case NOPOWER:
      code = -1006 
      message = "没有操作权限~"
      break
  }
  // 抛出错误
  ctx.body = { code, message }
})